package com.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.base.comm.CommContent;
import com.base.po.voip.Adversiments;
import com.base.service.AdversimentsService;
import com.base.service.LogsService;
import com.base.utils.IP;
import com.base.utils.Produce;

/**
 * 添加轮播广告信息服务端
 * 
 * @author f 优化日期：10-16
 *
 */
@Controller
@RequestMapping("/admin")
public class CarouselAddController {

	@Autowired
	private LogsService logsService; // 日志Service
	@Autowired
	private AdversimentsService adversimentsService; // 轮播Service

	// 添加轮播广告
	@RequestMapping(value = "/carousel_do_add", method = RequestMethod.POST)
	public @ResponseBody int doAdd(HttpServletRequest request, @RequestBody(required = false) Adversiments adversiments) {
		HttpSession session = request.getSession(false); // 获取session
		if (session == null) {
			return -1;
		}
		Object obj_id = session.getAttribute("adminUserId"); // 登录ID
		Object obj_name = session.getAttribute("userNick"); // 登录名称
		if (obj_id == null || obj_name == null) {
			return -1;
		}
		String userid = (String) obj_id;
		if (adversiments.getAdtype() == 1) { // 添加启动页 只能添加一张
			List<Adversiments> beanAds = adversimentsService.selectByAll(new Adversiments(userid, true, 1)); // 获取启动页
			if (beanAds != null && beanAds.size() > 0) {
				return -2; // 失败
			}
		}
		adversiments.setId(Produce.getUUID()); // 设置参数 UUID
		adversiments.setCreatetime(Produce.getNowDate()); // 时间
		adversiments.setParentid(userid); // 代理商ID
		adversiments.setDatastatus(true); // 数据状态
		logsService.insert(CommContent.TYPE_CREATE, CommContent.LOGS_XIAOXI + "配置轮播广告信息", (String) obj_name, true,
				IP.getIpAddr(request));
		return adversimentsService.insertSelective(adversiments); // 添加
	}
}
